body = $('body')

zindex = 1000

$(document).keyup (e) ->
    max = 0
    to_close = 0
    if e.keyCode == 27
        for i in $(".win")
            z = i.style.zIndex - 0
            if z > max and i._option_ and i._option_.esc
                max = z
                to_close = i
        if to_close
            to_close._win_close_()

_default = {
    esc:1
    close:'<i class="iconfont icon-close"></i>'
    className:"animated slideInRight"
}

_pre_url = location.pathname



$(window).on "popstate", ->
    p = location.pathname
    if p == _pre_url
        return
    _pre_url = p
    win = 0
    z = 0
    for i in $(".win")
        if $(i).find(".icon-close")[0]
            _z = i.style.zIndex
            if _z > z
                z = _z
                win = i
    if win
        win._win_close_()

$.win = (html, callback, option={})->
    _pre_url = location.pathname

    option = $.extend({},_default, option)
    if option.esc
        className = " esc"
    else
        className = ""
    win = $ """<div class="win#{className}"><style>#{__inline("css/lib/win.css")}</style><div class="html"></div></div>"""
    if option.className
        win.addClass(option.className)
    win.css('z-index', ++zindex)
    html_wrap = win.find(">.html").html(html)

    url = location.pathname.slice(1)
    URL.remember()

    vm_list = []
    
    _close = (back=1)->
        if option.on_close
            option.on_close.apply win
        if back
            URL.back(url)
        win.removeClass('slideInRight').addClass('fadeOut animated')
        setTimeout(
            ->
                win.remove()
                for i in vm_list
                    delete avalon.vmodels[i]
                if not $('.win').length
                    body.css('overflow','')
                    
            290
        )
        
    
    win0 = win[0]
    if option.close
        win.prepend $(option.close).click(_close)
        if option.esc
            win0._option_ = {esc:option.esc}

    win0._win_close_ = html_wrap.close = _close

    html_wrap.def = (_obj)->
        for id, obj of _obj
            obj.$id = id
            vm = avalon.define(obj)
            if obj.submit
                func = obj.submit
                submit = (e)->

                    e.preventDefault()
                    r = func()
                    if r and r.then
                        timeout = setTimeout(
                            ->
                                vm.submit = submit
                            7000
                        )
                        _clear = ->
                            clearTimeout(timeout)
                            vm.submit = submit
                        r.then(
                            _clear
                            (err)->
                                _clear()
                                $.errtip(vm).set(err)
                        )
                        vm.submit = (e)->
                            e.preventDefault()

                vm.submit = submit
            vm_list.push obj.$id
            avalon.scan()
            return vm
    
    html_wrap.bk = (_obj)->
        $.bk(html_wrap, _obj)

    body.css('overflow','hidden').append win
    callback? html_wrap
    if vm_list.length
        avalon.scan()

